<template lang="html">
  <div>
    <div class="search">
        <div class="input">
             <p><label>订单编号：</label><el-input placeholder="请输入内容" v-model="pageData.name"></el-input></p>
             <p><label>手机号：</label><el-input placeholder="请输入内容" v-model="pageData.phone"></el-input></p>
             <p><label>订单类型：</label>
               <el-select v-model="pageData.consignType" placeholder="请选择" @change="getType">
                   <el-option value="">请选择</el-option>
                  <el-option
                    v-for="item in type"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
             </el-input></p>
             <p><label>订单状态：</label>
               <el-select v-model="pageData.orderStatus" placeholder="请选择">
                 <el-option value=''>请选择</el-option>
                  <el-option
                    v-for="item in state"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
             </el-input></p>
             <p><el-button type="primary" class="se" style="margin-left:10px;" @click="search">搜索</el-button></p>
        </div>
    </div>
    <tableVue :list="list" :state="false" v-loading="pageData.loading" :total="total" @sizeChange="handSize">
        <slot>
          <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  @click="details(scope.row)">详情
                </el-button>
                <el-button
                 v-if="scope.row.orderStatus == '已出库'"
                  size="mini"
                  type="success"
                  @click="deliver(scope.row)">发货
                </el-button>
                <el-button
                 v-if="scope.row.orderStatus == '待出库'"
                  size="mini"
                  type="success"
                  @click="delivery(scope.row)">出库
                </el-button>
              </template>
          </el-table-column>
        </slot>
    </tableVue>
    <dialogVue title="添加发货地址" :dialog="dialog" @close="changeData" :state="false">
        <slot>
            <el-form :model="pageData" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
                <el-form-item label="快递单号:" prop="logisticsNo">
                  <el-input v-model="pageData.logisticsNo" placeholder="请输入内容"></el-input>
                  <el-button type="primary" :loading="loading" @click="getlogistId">查询快递公司</el-button>
                </el-form-item>
                <el-form-item label="快递公司" prop="logisticsCompanyCode">
                    <el-select v-model="pageData.logisticsCompanyCode" placeholder="请选择" :disabled="disableda">
                        <el-option value="">请选择</el-option>
                        <el-option
                          v-for="(item,key) in logistics"
                          :key="key"
                          :label="item.companyName"
                          :value="item.companyCode">
                        </el-option>
                      </el-select>
                 </el-form-item>
                 <el-form-item class="confirm">
                    <el-button type="primary"  @click="submitForm">确定</el-button>
                 </el-form-item>
            </el-form>
        </slot>
    </dialogVue>
  </div>
</template>

<script>
  import Data from './ransomIndentData.js'
  import tableVue from '@/components/alltable.vue'
  import OrderDetailVue from '@/components/orderDetail.vue'
  import dialogVue from '@/components/dialog.vue'
  import {
    deliveryOut,
    shipments
  } from "@/api/product/ransomIndent";
  import {
    ransomIndentList
  } from "@/api/product/consignIndent";
  import {
    getLogisticsInfo
  } from "@/api/common/upImg";
  export default {
    components : {
      tableVue,
      OrderDetailVue,
      dialogVue
    },
    data () {
       return JSON.parse(JSON.stringify(Data))
    },
    mounted () {
      this.getransomIndentList();
    },
    methods : {
      details (item) {
        this.$router.push({
          name :'details',
          query : {
            id : item.orderId
          }
        })
      },
      getransomIndentList (data) {
        this.pageData.loading = true;
        ransomIndentList (data).then(res=>{
          if (res.data.code == 'OK') {
            this.pageData.loading = false;
            this.total = res.data.data.total;
            this.list.tbody = res.data.data.records.map(item=>{
              if (item.consignType == 'MEN_DIAN') {
                  item.consignType = '去门店'
                  switch (item.orderStatus) {
                    case 0 : item.orderStatus = '等待用户到店'
                      break;
                    case 1 : item.orderStatus = '用户到店'
                      break
                    case 4 : item.orderStatus = '订单完成（待打款）'
                      break
                    case 5 : item.orderStatus = '打款完成（可赎回）'
                      break
                    case 9 : item.orderStatus = '已取消'
                      break
                    case 10 : item.orderStatus = '待出库'
                      break
                    case 11 : item.orderStatus = '已出库'
                      break
                    case 12 : item.orderStatus = '待收货'
                      break
                    case 13 : item.orderStatus = '已赎回'
                      break
                    case 14 : item.orderStatus = '到期不可赎回'
                      break
                    default:
                  }
              } else if (item.consignType == 'SHANG_MEN') {
                  item.consignType = '上门'
                  switch (item.orderStatus) {
                    case 0 : item.orderStatus = '等待评估师上门'
                      break;
                    case 1 : item.orderStatus = '评估师已上门'
                      break
                    case 4 : item.orderStatus = '订单完成（待打款）'
                      break
                    case 5 : item.orderStatus = '打款完成（可赎回）'
                      break
                    case 9 : item.orderStatus = '已取消'
                      break
                    case 10 : item.orderStatus = '待出库'
                      break
                    case 11 : item.orderStatus = '已出库'
                      break
                    case 12 : item.orderStatus = '待收货'
                      break
                    case 13 : item.orderStatus = '已赎回'
                      break
                    case 14 : item.orderStatus = '到期不可赎回'
                      break
                    default:
                  }
              } else if (item.consignType == 'KUAIDI') {
                item.consignType = '快递'
                switch (item.orderStatus) {
                  case 0 : item.orderStatus = '待收货'
                    break;
                  case 1 : item.orderStatus = '已收货'
                    break
                  case 2 : item.orderStatus = '待估价'
                    break
                  case 3 : item.orderStatus = '已估价（用户可取消）'
                    break
                  case 4 : item.orderStatus = '订单完成（待打款）'
                    break
                  case 5 : item.orderStatus = '打款完成（可赎回）'
                    break
                  case 6 : item.orderStatus = '用户不同意'
                    break
                  case 7 : item.orderStatus = '待收货（用户不同意时）'
                    break
                  case 8 : item.orderStatus = '已完成（用户不同意时）'
                    break
                  case 10 : item.orderStatus = '待出库'
                    break
                  case 11 : item.orderStatus = '已出库'
                    break
                  case 12 : item.orderStatus = '待收货'
                    break
                  case 13 : item.orderStatus = '已赎回'
                    break
                  case 14 : item.orderStatus = '到期不可赎回'
                    break
                  default:
                }
              } else {
                item.consignType = '图片估计'
                switch (item.orderStatus) {
                  case 0 : item.orderStatus = '待估价'
                    break;
                  case 1 : item.orderStatus = '已估价'
                    break
                  case 10 : item.orderStatus = '待出库'
                    break
                  case 11 : item.orderStatus = '已出库'
                    break
                  case 12 : item.orderStatus = '待收货'
                    break
                  case 13 : item.orderStatus = '已赎回'
                    break
                  case 14 : item.orderStatus = '到期不可赎回'
                    break
                  default:
                }
              }
               item.gmtCreate = this.switchTime(item.gmtCreate)
               item.gmtUpdate = this.switchTime(item.gmtUpdate)
               return item;
            });
             this.total = res.data.data.total;
          } else {
            this.$message.error(res.data.message);
          }
        })
      },
      search () {
        var data = {
          contactTel : this.pageData.phone,
          consignType : this.pageData.consignType,
          orderStatus : this.pageData.orderStatus,
          orderNo : this.pageData.name,
        }
        this.getransomIndentList(data);
      },
      changeData (data) {
        this.dialog = data;
      },
      getType (val) {
        switch (val) {
          case this.obj['SHANG_MEN'].key : this.state = this.obj[val].value;
            break;
          case this.obj['MEN_DIAN'].key : this.state = this.obj[val].value;
            break;
          case this.obj['KUAIDI'].key : this.state = this.obj[val].value;
              break;
          case this.obj['PREV_PIC'].key : this.state = this.obj[val].value;
            break;
          default: this.state = []
        }
      },
      handSize (val) {
        var data = {
          contactTel : this.pageData.phone,
          consignType : this.pageData.consignType,
          orderStatus : this.pageData.orderStatus,
          orderNo : this.pageData.name,
          current : val
        }
        this.getransomIndentList(data);
      },
      delivery (item) {//出库
        this.$confirm('此操作将把商品出库, 是否继续?', '提示', {
           confirmButtonText: '确定',
           cancelButtonText: '取消',
           type: 'warning'
         }).then(() => {
           deliveryOut(item.orderId).then(res=>{
             if (res.data.code == 'OK') {
               this.$message({
                  message: '出库成功',
                  type: 'success'
                });
                this.getransomIndentList();
             }
           })
         }).catch(() => {
           this.$message({
             type: 'info',
             message: '已取消'
           });
         });
      },
      getlogistId () {//获取物流信息
         if (this.pageData.logisticsNo) {
           this.loading = true;
           getLogisticsInfo(this.pageData.logisticsNo).then(res=>{
              if (res.data.code == 'OK') {
                  this.logistics = res.data.data;
                  this.disableda = false;
                   this.loading = false;
              } else {
                this.$message.error(res.data.message)
                this.loading = false;
              }
           })
         } else {
           this.$message.error('请输入快递单号!!')
         }
      },
      submitForm () {
        this.$refs.ruleForm.validate((valid) => {
           if (valid) {
             this.$confirm('此操作将把商品发货, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                shipments(this.pageData.orderid,this.pageData.logisticsNo,this.pageData.logisticsCompanyCode).then(res=>{
                  if (res.data.code == 'OK') {
                    this.dialog = false;
                    this.$message({
                       message: '发货成功',
                       type: 'success'
                     });
                     this.getransomIndentList();
                  } else {
                      this.$message.error(res.data.message)
                  }
                })
              }).catch(() => {
                this.$message({
                  type: 'info',
                  message: '已取消'
                });
              });
           } else {
             console.log('error submit!!');
             return false;
           }
         });

      },
      deliver (item) {//发货
        this.dialog = true;
        this.pageData = {};
        this.pageData.orderid = item.orderId
      },
    }
  }
</script>

<style scoped>
  @import "../../../styles/common.css";
</style>
